package com.ftg.learn.day03;

import java.util.HashSet;
import java.util.Set;

/**
 * 青蛙: 7 3 1 --> 几次
 * 1 : 二次使用函数
 *
 * @author 布衣
 */
public class FrogJump {

    /**
     * 做一道题，青蛙跳井问题
     *
     * @param wellHeight 井深
     * @param oneHeight  一次跳多高
     * @param downHeight 下滑几米
     * @return int 表示次数
     */
    public int jumpWell(int wellHeight, int oneHeight, int downHeight) {
        if (wellHeight < 0 || oneHeight < 0 || downHeight < 0) {
            throw new RuntimeException("对不起，参数传递错误,数值不能小于0");
        }
        //临界问题  (容错处理)
        if (wellHeight < oneHeight) {
            return 1;
        }

        if (oneHeight < downHeight) {
            //异常处理
            throw new RuntimeException("对不起，参数传递错误，下滑不应该高过跳的高度");
        }

        int n = 0;
        // 逻辑处理  for变形
        for (int i = 0; ; ) {
            i += oneHeight;
            n++;
            if (i >= wellHeight) {
                //跳出循环
                break;
            } else {
                i -= downHeight;
            }
        }
        return n;
    }

    /**
     * 做一道题，青蛙跳井问题
     *
     * @param wellHeight 井深
     * @param oneHeight  一次跳多高
     * @param downHeight 下滑几米
     * @return int 表示次数
     */
    public int jumpWell02(int wellHeight, int oneHeight, int downHeight) {
        if (wellHeight < 0 || oneHeight < 0 || downHeight < 0) {
            throw new RuntimeException("对不起，参数传递错误,数值不能小于0");
        }
        //临界问题  (容错处理)
        if (wellHeight < oneHeight) {
            return 1;
        }

        if (oneHeight < downHeight) {
            //异常处理
            throw new RuntimeException("对不起，参数传递错误，下滑不应该高过跳的高度");
        }

        int n = 0;
        int position = 0;
        while (position < wellHeight) {
            if (n != 0) {
                position -= downHeight;
            }
            position += oneHeight;
            n++;
        }
        return n;
    }

    /**
     * 做一道题，青蛙跳井问题
     *
     * @param wellHeight 井深
     * @param oneHeight  一次跳多高
     * @param downHeight 下滑几米
     * @return int 表示次数
     */
    public int jumpWell01(int wellHeight, int oneHeight, int downHeight) {
        if (wellHeight < 0 || oneHeight < 0 || downHeight < 0) {
            throw new RuntimeException("对不起，参数传递错误,数值不能小于0");
        }
        //临界问题  (容错处理)
        if (wellHeight < oneHeight) {
            return 1;
        }

        if (oneHeight < downHeight) {
            //异常处理
            throw new RuntimeException("对不起，参数传递错误，下滑不应该高过跳的高度");
        }

        int n = 1;
        // 逻辑处理  for变形
        for (int i = oneHeight; i < wellHeight; i += oneHeight) {
            i -= downHeight;
            n++;
        }
        return n;
    }

    public int jumpWell03(int wellHeight, int oneHeight, int downHeight) {
        if (wellHeight < 0 || oneHeight < 0 || downHeight < 0) {
            throw new RuntimeException("对不起，参数传递错误,数值不能小于0");
        }
        //临界问题  (容错处理)
        if (wellHeight < oneHeight) {
            return 1;
        }

        if (oneHeight < downHeight) {
            //异常处理
            throw new RuntimeException("对不起，参数传递错误，下滑不应该高过跳的高度");
        }

        int n = 0;
        int position = 0;
        do {
            if (n != 0) {
                position -= downHeight;
            }
            position += oneHeight;
            n++;
        } while (position < wellHeight);
        return n;
    }

    public static void main(String[] args) {
        //三种  for while  do...while
        FrogJump frog1 = new FrogJump();
        int i = frog1.jumpWell03(7, 3, 1);
        System.out.println(i);

    }

}
